package cn.appinfo.controller.backend;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appinfo.pojo.BackendUser;
import cn.appinfo.service.backendUser.BackendUserService;

@Controller
@RequestMapping("/manager")
public class BackendUserController {
private Logger logger = Logger.getLogger(BackendUserController.class);
	
	@Resource
	private BackendUserService backendUserService;
	
	@RequestMapping(value="/login")
	public String login(){
		return "backendlogin";
	}
	
	@RequestMapping(value="/dologin",method=RequestMethod.POST)
	public String dologin(@RequestParam String userCode,@RequestParam String userPassword,
				HttpServletRequest request,HttpSession session){
		BackendUser backendUser=backendUserService.login(userCode);
		if(null != backendUser){//登录成功
			if(backendUser.getUserPassword().equals(userPassword)){
				//放入session
				session.setAttribute("userSession", backendUser);
				//页面跳转（main.jsp）
				return "redirect:/manager/backend/main";
			}else{
				//页面跳转（login.jsp）带出提示信息--转发
				request.setAttribute("error", "密码不正确");
				return "backendlogin";
			}
		}else{
			//页面跳转（login.jsp）带出提示信息--转发
			request.setAttribute("error", "用户名不正确");
			return "backendlogin";
		}
	}
	
	@RequestMapping("/backend/main")
	public String main(HttpSession session){
		if(session.getAttribute("userSession") == null){
			return "redirect:/manager/login";
		}
		return "backend/main";
	}
	
	@RequestMapping("/logout")
	public String logmain(HttpSession session){
		session.removeAttribute("userSession");
		return "redirect:/manager/login";
	}
}
